package org.dromara.demo.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.domain.BaseEntity;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.demo.domain.bo.JsExecuteBo;
import org.dromara.demo.domain.vo.JsExecuteVo;
import org.dromara.demo.domain.JsExecute;
import org.dromara.demo.mapper.JsExecuteMapper;
import org.dromara.demo.service.IJsExecuteService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * executeService业务层处理
 *
 * @author zhu
 * @date 2025-01-02
 */
@RequiredArgsConstructor
@Service
public class JsExecuteServiceImpl implements IJsExecuteService {

    private final JsExecuteMapper baseMapper;

    /**
     * 查询execute
     *
     * @param id 主键
     * @return execute
     */
    @Override
    public JsExecuteVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询execute列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return execute分页列表
     */
    @Override
    public TableDataInfo<JsExecuteVo> queryPageList(JsExecuteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JsExecute> lqw = buildQueryWrapper(bo);
        Page<JsExecuteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的execute列表
     *
     * @param bo 查询条件
     * @return execute列表
     */
    @Override
    public List<JsExecuteVo> queryList(JsExecuteBo bo) {
        LambdaQueryWrapper<JsExecute> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JsExecute> buildQueryWrapper(JsExecuteBo bo) {
        LambdaQueryWrapper<JsExecute> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSuiteId() != null, JsExecute::getSuiteId, bo.getSuiteId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), JsExecute::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getSuiteName()), JsExecute::getSuiteName, bo.getSuiteName());
        lqw.orderByDesc(JsExecute::getCreateTime);
        return lqw;
    }

    /**
     * 新增execute
     *
     * @param bo execute
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JsExecuteBo bo) {
        JsExecute add = MapstructUtils.convert(bo, JsExecute.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改execute
     *
     * @param bo execute
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JsExecuteBo bo) {
        JsExecute update = MapstructUtils.convert(bo, JsExecute.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JsExecute entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除execute信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public JsExecuteVo queryLast() {
        JsExecuteVo jsExecuteVo = baseMapper.selectVoOne(new LambdaQueryWrapper<JsExecute>().eq(BaseEntity::getCreateBy, LoginHelper.getUserId()).orderByDesc(JsExecute::getCreateTime).last("limit 1"));
        return jsExecuteVo;
    }
}
